package edu.upc.rentacar.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import edu.upc.rentacar.domain.Auto;
import edu.upc.rentacar.domain.AutoList;
import edu.upc.rentacar.service.AutoService;

@Controller
public class AutoController {

	protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name = "autoService")
	private AutoService autoService;
	
	@RequestMapping(value = "/auto/{id}", method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
	public @ResponseBody Auto getAuto(@PathVariable("id") Long id) {
		logger.debug("Provider has received request to get car with id: "
				+ id);

		// Call service here
		return autoService.get(id);
	}
	
	@RequestMapping(value = "/auto/{id}", method = RequestMethod.GET, headers = "Accept=application/html, application/xhtml+xml")
	public String getAutoHTML(@PathVariable("id") Long id, Model model) {
		logger.debug("Provider has received request to get car with id: "
				+ id);

		// Call service to here
		model.addAttribute("auto", autoService.get(id));

		// This will resolve to /WEB-INF/jsp/getpage.jsp
		return "getpage";
	}
	
	@RequestMapping(value = "/autos", method = RequestMethod.GET, headers = "Accept=application/xml, application/json")
	public @ResponseBody AutoList getAuto() {
		logger.debug("Provider has received request to get all cars");

		// Call service here
		AutoList result = new AutoList();
		
		result.setData(autoService.getAll());

		return result;
	}
	
}
